package com.garrigou.spaceship.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.World;
import com.garrigou.spaceship.SpaceShip;
import com.garrigou.spaceship.actor.Ship2D;
import com.garrigou.spaceship.camera.OrthoCamController;
import com.garrigou.spaceship.camera.ParallaxCamera;
import com.garrigou.spaceship.director.Director;
import com.garrigou.spaceship.domain.Level;
import com.garrigou.spaceship.domain.Profile;
import com.garrigou.spaceship.event.ActorEvent;
import com.garrigou.spaceship.event.ActorEventObserver;
import com.garrigou.spaceship.scenes.GameScene;
import com.garrigou.spaceship.services.MusicManager.SpaceShipMusic;

public class LevelScreen extends AbstractScreen implements ActorEventObserver
{
    private final Profile  profile;
    private final Level    level;

    private Ship2D         ship2d;
    // The one and only director.
    private final Director director;

    public LevelScreen(SpaceShip game, int targetLevelId)
    {
        super(game);

        // set the basic attributes
        profile = game.getProfileManager().retrieveProfile();
        level = game.getLevelManager().findLevelById(targetLevelId);

        // Requires Graphics context.
        this.director = Director.instance();

// director.setClearColourA(DEFAULT_CLEAR_COLOUR_ALPHA);
// director.setClearColourR(DEFAULT_CLEAR_COLOUR_RED);
// director.setClearColourG(DEFAULT_CLEAR_COLOUR_GREEN);
// director.setClearColourB(DEFAULT_CLEAR_COLOUR_BLUE);

        // Set initial width and height.
        this.director.setWidth(GAME_VIEWPORT_WIDTH);
        this.director.setHeight(GAME_VIEWPORT_HEIGHT);

        // Add this as an event observer.
        this.director.registerEventHandler(this);

        this.director.setScene(new GameScene(profile, getAtlas()));
    }

    private World   world;
    private Body    ennemy;
    private Vector2 ennemyModelOrigin;

// private void Enemy()
// {
// // 0. Create a loader for the file saved from the editor.
// BodyEditorLoader loader = new BodyEditorLoader(Gdx.files.internal("data/test.json"));
//
// // 1. Create a BodyDef, as usual.
// BodyDef bd = new BodyDef();
// bd.type = BodyType.DynamicBody;
//
// // 2. Create a FixtureDef, as usual.
// FixtureDef fd = new FixtureDef();
// fd.density = 1;
// fd.friction = 0.5f;
// fd.restitution = 0.3f;
//
// // 3. Create a Body, as usual.
// ennemy = world.createBody(bd);
//
// // 4. Create the body fixture automatically by using the loader.
// loader.attachFixture(ennemy, "test01", fd, BOTTLE_WIDTH);
// ennemyModelOrigin = loader.getOrigin("test01", BOTTLE_WIDTH).cpy();
// }

    @Override
    protected boolean isGameScreen()
    {
        return true;
    }

    @Override
    public void show()
    {
        super.show();
        game.getMusicManager().play(SpaceShipMusic.LEVEL);
    }

//
// // Add the parallax background
// stage.addActor(controller);
//
// // play the level music
// game.getMusicManager().play(SpaceShipMusic.LEVEL);
//
// // create the ship and add it to the stage
// ship2d = Ship2D.create(profile.getShip(), getAtlas());
// controller.setShip(ship2d);
//
// // center the ship horizontally
// ship2d.setInitialPosition((stage.getWidth() / 2 - ship2d.getWidth() / 2), ship2d.getHeight());
//
// // add the ship to the stage
// stage.addActor(ship2d);
//
// // add a fade-in effect to the whole stage
// stage.getRoot().getColor().a = 0f;
// stage.getRoot().addAction(Actions.fadeIn(0.5f));
// }

  

    @Override
    public void render(float delta)
    {
// // TODO Auto-generated method stub
// super.render(delta);

        // Update director
        director.update();
    }

    static final String TAG = LevelScreen.class.getSimpleName();

    @Override
    public boolean handleEvent(ActorEvent event)
    {
        return false;
    }
}
